﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;




using System.IO;
using MySql.Data.MySqlClient;
using System.Data.OleDb;
using h = functionService.shorts;
using da = functionService.DATECOD;
using fr = HK.FiscalRegistrar.FiscalRegistrar;
using vv = functionService.serfun_city7;
using rz = functionService.serfun_city7.rz;
using nameCod = functionService.nameCod;


namespace city7
{
    public partial class FormSirTehSir : Form
    {
        functionService.prm_city q;
        city7.rzpt.modeWAED  currentMode;
        DataTable dts; 

        public FormSirTehSir()
        {
            InitializeComponent(); InitializeComponentMy();
        }

        private void InitializeComponentMy()
        {
            
            q = new functionService.prm_city();

            string s = q.tk((int)ETALON.param.server_1);
            q.con.co(s);
            refresh();
           // dataGridView1.DataSource = dts;
            set();
            setMode(city7.rzpt.modeWAED.work);

            vv.sirTehSir.currentColumn = 0;
            vv.sirTehSir.currentRow = 0;
            vv.sirTehSir.trueSet_from_CurrentTehSir = false;
            
            vv.sirTehSir.trueMemo = true;
            toolStripStatusLabel2.Text = "MEMO";
            vv.sirTehSir.ep = 1;
            toolStripStatusLabel1.Text = "1";
            q.put(64, 0); vv.sirTehSir.trueMemo = false; toolStripStatusLabel2.Text = "No Memo";

           // if (q.tk(64) == "1") { vv.sirTehSir.trueMemo = true; toolStripStatusLabel2.Text = "Memo"; } else { vv.sirTehSir.trueMemo = false; toolStripStatusLabel2.Text = "No Memo"; }
        }
        void refresh2(string whereNameLike)
        {
            //select name,namets,ep from q1 where name rlike(if(isnull(whereName),'',whereName)) order by name;

            DataView dv = new DataView(dts);
            dv.RowFilter = string.Concat("namets like '%", toolStripTextBox2.Text, "%'");
            dataGridView1.DataSource = dv;

        }
        private void toolStripTextBox1KeyUp()
        {
            DataView dv = new DataView(dts); //H.m(30);
            dv.RowFilter = string.Concat("name like '%", toolStripTextBox1.Text, "%'");
            dataGridView1.DataSource = dv;
        }
        void refresh(string whereNameLike)
        {
            if (whereNameLike != null)
            {
                if (whereNameLike.Length > 0)
                {
                    //dts = q.selectAny(h.a("call `city`.`select_sir_tehSirW`('", whereNameLike, "');"));
                    toolStripTextBox1KeyUp();
                   // dataGridView1.DataSource = dts;
                }
                else
                {
                    dts = q.selectAny("call `city`.`select_sir_tehSir`();");
                    dataGridView1.DataSource = dts;
                }
            }
        }
        void refresh()
        {
            dts = q.selectAny("call `city`.`select_sir_tehSir`();");
            dataGridView1.DataSource = dts;
        }
        void refresh(int xxx, int yyy)
        {
            dts = q.selectAny("call `city`.`select_sir_tehSir`();");
            dataGridView1.DataSource = dts;
            dataGridView1.CurrentCell = dataGridView1[xxx, yyy];
        }
        void setMode(rzpt.modeWAED mo)
        {
            currentMode = mo;
            if (mo == city7.rzpt.modeWAED.work)
            {
                setColor(Color.LightGray, Color.White);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                refresh();
            }
            else if (mo == city7.rzpt.modeWAED.add)
            {
                setColor(Color.SteelBlue, Color.LightSkyBlue);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;

                cellclick_add();
            }
            else if (mo == city7.rzpt.modeWAED.edit)
            {
                setColor(Color.Green, Color.LightGreen);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;

               // cellclick_edit();
            }
            else if (mo == city7.rzpt.modeWAED.delete)
            {
                setColor(Color.Coral, Color.Coral);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            else if (mo == city7.rzpt.modeWAED.non)
            {
                setColor(Color.LightGray, Color.White);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }

        }

        void set()
        {
            dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.LightGray;
            
            dataGridView1.Columns[0].Width = 330;
            dataGridView1.Columns[1].Width = 330;

            dataGridView1.Columns[0].HeaderText = "Наименование Сырья Склад";
            dataGridView1.Columns[1].HeaderText = "Наименование Сырья Техн";

        }

        void setColor(Color c1, Color c2)
        {
            dataGridView1.Columns[0].DefaultCellStyle.BackColor = c1;
            dataGridView1.Columns[1].DefaultCellStyle.BackColor = c2;
        }


        void cellclick()
        {
            if (currentMode == city7.rzpt.modeWAED.add)
            {
                cellclick_add();
            }
            else if (currentMode == city7.rzpt.modeWAED.edit)
            {
                cellclick_edit();
            }
            else if (currentMode == city7.rzpt.modeWAED.delete)
            {
                cellclick_del();
            }
            else if (currentMode == city7.rzpt.modeWAED.work)
            {
                cellclick_work();
            }
            else if (currentMode == city7.rzpt.modeWAED.non)
            {
                cellclick_non();
            }
        }
        private void cellclick_add()
        {
            //int i = nameCod.readCod_int(SELECTprod);
            //q.UpdateData(rz.sql.add(i, SELECTkolV)); refresh(i);
        }
        private string SELECTtehSirCod
        {
            get
            {
                string s = "- (888)";
                    ETALON.listAnyEdit f =
                        new ETALON.listAnyEdit("city", "list_sc", "cod>0"); f.ShowDialog();
                    if (f.IsOK )
                    {
                        s = f.sCod; vv.sirTehSir.currentTehSir_nameCod  = f.sText; toolStripStatusLabel1.Text = f.sText;
                    }
                    vv.sirTehSir.trueSet_from_CurrentTehSir = true ;
                return s;
            }
        }
        private string SELECTep
        {
            get
            {

                if (vv.sirTehSir.trueSet_from_CurrentTehSir == false)
                {
                    ETALON.FormKolZakaz fk = new ETALON.FormKolZakaz("Введите число", "1");
                    fk.ShowDialog();
                    if (fk.IsSelect)
                    {

                        if (vv.sirTehSir.trueMemo)
                        {
                            vv.sirTehSir.ep = Convert.ToDouble(fk.valueEnter); toolStripStatusLabel1.Text = fk.valueEnter.ToString();
                        }
                        if (q.tk(64) == "1") { vv.sirTehSir.trueSet_from_CurrentTehSir = true; }
                        else { vv.sirTehSir.trueSet_from_CurrentTehSir = false ; } 
                        
                        return fk.valueEnter.ToString();
                    }
                    else
                    {
                        return "1";
                    }
                }
                else
                {
                    return vv.sirTehSir.ep.ToString();
                }
            }
        }



        private void cellclick_edit()
        {
            int xx = dataGridView1.SelectedCells[0].ColumnIndex;
            object y = dataGridView1.SelectedCells[0].Value;
            int yy = dataGridView1.SelectedCells[0].RowIndex;
            object ov = "";
            string codScSir = nameCod.readCod(dataGridView1.Rows[yy].Cells[0].Value.ToString());

            string sql = "", s_SELECTtehSirCod=""; 

            if (xx == 0) { }  //наименование сырья склад
            else if (xx == 1)  //наименование тех сырья
            {
                if (vv.sirTehSir.trueSet_from_CurrentTehSir == false)
                {
                    vv.sirTehSir.CurrentTehSir_cod = SELECTtehSirCod;
                    sql = string.Concat("update sir_listsir_ts set codTs = ", vv.sirTehSir.CurrentTehSir_cod, " where cod=", codScSir, ";");
                }
                else
                {
                    if (q.tk(64) == "1")
                    {
                        sql = string.Concat("update sir_listsir_ts set codTs = ", vv.sirTehSir.CurrentTehSir_cod, " where cod=", codScSir, ";");
                    }
                    else
                    {
                        vv.sirTehSir.CurrentTehSir_cod = SELECTtehSirCod;
                        sql = string.Concat("update sir_listsir_ts set codTs = ", vv.sirTehSir.CurrentTehSir_cod, " where cod=", codScSir, ";"); 
                    }
                }
                q.UpdateData(sql);
                //vv.sirTehSir.currentTehSir_nameCod
                dataGridView1.SelectedCells[0].Value = vv.sirTehSir.currentTehSir_nameCod;
               // refresh(xx,yy);
            }
            else if (xx == 2)  //единицы преобр
            {
                string ep = "";
                if (vv.sirTehSir.trueSet_from_CurrentTehSir == false)
                {
                    ep = SELECTep;
                }
                else
                {
                    ep = vv.sirTehSir.ep.ToString();
                }
                //sql = string.Concat("update sir_listsir_ts set ep = ", ep, " where cod=", codScSir, ";");

                sql = string.Concat("update sir_listsir_ts set ep = ", ep, " where cod=", codScSir, ";");

                    q.UpdateData(sql);
                    dataGridView1.SelectedCells[0].Value = ep;
              //  refresh(xx, yy);
            }
            
        }

        private void editValue()
        {
        }

        private void cellclick_del()
        {
            //vv.rz.currentCod123 = 0;
            //vv.rz.currentCod123 = dataGridView1.SelectedRows[0].Cells[4].Value; //h.m(o123);
            //vv.rz.currentCodSc = q.selectScalar(
            //   string.Concat("select codSc from mainlistr where cod123 = ", vv.rz.currentCod123, ";"));

            //q.UpdateData(rz.sql.del(Convert.ToInt32(vv.rz.currentCod123))); refresh(h.i(vv.rz.currentCodSc));
        }
        private void cellclick_work()
        {

        }
        private void cellclick_non()
        {

        }

        private void setCurrent()
        {
            
        }
        private void toolStripDropDownButton3_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.work);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.add );
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.delete );
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.edit);
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            
            refresh(toolStripTextBox1.Text);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cellclick();
        }

        private void toolStripStatusLabel1_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel1.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            vv.sirTehSir.trueSet_from_CurrentTehSir = false;
            toolStripStatusLabel1.Text = "- (888)"; vv.sirTehSir.currentTehSir_nameCod = "- (888)";
            vv.sirTehSir.ep = 1; 
        }

        

        private void toolStripStatusLabel2_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel2.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            if (q.tk(64) == "1")  // if (vv.sirTehSir.trueMemo)
            {
                vv.sirTehSir.trueMemo = false;
                q.put(64, 0);
                toolStripStatusLabel2.Text = "No MEMO";
            }
            else
            {
                vv.sirTehSir.trueMemo = true ;
                toolStripStatusLabel2.Text = "MEMO"; q.put(64, 1);
            }
        }

        private void toolStripStatusLabel1_MouseDown(object sender, MouseEventArgs e)
        {
            //toolStripStatusLabel1.DisplayStyle = ToolStripItemDisplayStyle.None ;
        }

        private void toolStripStatusLabel2_MouseDown(object sender, MouseEventArgs e)
        {
           // toolStripStatusLabel2.DisplayStyle = ToolStripItemDisplayStyle.None;
        }

        private void toolStripTextBox2_KeyUp(object sender, KeyEventArgs e)
        {

            refresh2(toolStripTextBox2.Text);
        }

        private void toolStripStatusLabel2_Click(object sender, EventArgs e)
        {

        }

        
    }
}
